import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class money {
    public money() {}

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BillManager billManager = new BillManager();

        while (true) {
            System.out.println("请选择操作：");
            System.out.println("1. 添加收入");
            System.out.println("2. 添加支出");
            System.out.println("3. 查询收入");
            System.out.println("4. 查询支出");
            System.out.println("5. 查看所有账单");
            System.out.println("6. 查询账单");
            System.out.println("7. 设置月度预算");
            System.out.println("8. 查看月度统计");
            System.out.println("9. 退出");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    addIncome(billManager, scanner);
                    break;
                case 2:
                    addExpense(billManager, scanner);
                    break;
                case 3:
                    queryIncomes(billManager);
                    break;
                case 4:
                    queryExpenses(billManager);
                    break;
                case 5:
                    viewAllBills(billManager);
                    break;
                case 6:
                    searchBillById(billManager, scanner);
                    break;
                case 7:
                    setMonthlyBudget(billManager, scanner);
                    break;
                case 8:
                    monthlyStatistics(billManager);
                    break;
                case 9:
                    System.out.println("退出程序！");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }
    private static void setMonthlyBudget(BillManager billManager, Scanner scanner) {
        System.out.print("请输入月度预算: ");
        double budget = scanner.nextDouble();
        scanner.nextLine();
        billManager.setMonthlyBudget(budget);
        System.out.println("月度预算已设置为: " + budget);
    }

    private static void monthlyStatistics(BillManager billManager) {
        billManager.monthlyStatistics();
    }
    private static void addIncome(BillManager billManager, Scanner scanner) {
        System.out.print("请输入日期（格式：YYYY-MM-DD）: ");
        String dateString = scanner.nextLine();
        LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE);
        System.out.print("请输入金额: ");
        double amount = scanner.nextDouble();
        scanner.nextLine();
        System.out.print("请输入类别: ");
        String category = scanner.nextLine();
        System.out.print("请输入备注: ");
        String note = scanner.nextLine();
        Income income = new Income(date, amount, category, note);
        billManager.addIncome(income);
        System.out.println("收入记录已添加！");
    }

    private static void addExpense(BillManager billManager, Scanner scanner) {
        System.out.print("请输入日期（格式：YYYY-MM-DD）: ");
        String dateString = scanner.nextLine();
        LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE);
        System.out.print("请输入金额: ");
        double amount = scanner.nextDouble();
        scanner.nextLine();
        System.out.print("请输入类别: ");
        String category = scanner.nextLine();
        System.out.print("请输入备注: ");
        String note = scanner.nextLine();
        Expense expense = new Expense(date, amount, category, note);
        billManager.addExpense(expense);
        System.out.println("支出记录已添加！");
    }

    private static void queryIncomes(BillManager billManager) {
        List<Income> incomes = billManager.getIncomes();
        System.out.println("收入记录:");
        for (Income income : incomes) {
            System.out.println(income.getDate() + " " + income.getAmount() + " " + income.getCategory() + " " + income.getNote());
        }
    }

    private static void queryExpenses(BillManager billManager) {
        List<Expense> expenses = billManager.getExpenses();
        System.out.println("支出记录:");
        for (Expense expense : expenses) {
            System.out.println(expense.getDate() + " " + expense.getAmount() + " " + expense.getCategory() + " " + expense.getNote());
        }
    }

    private static void viewAllBills(BillManager billManager) {
        List<Bill> bills = billManager.getAllBills();
        if (bills.isEmpty()) {
            System.out.println("没有账单记录。");
        } else {
            System.out.println("所有账单记录:");
            for (Bill bill : bills) {
                LocalDate date = LocalDate.parse(bill.getDate());
                if (date.getMonthValue() == 5) {
                    continue; // 跳过5月份的账单
                }
                System.out.println(bill);
            }
        }
    }

    private static void searchBillById(BillManager billManager, Scanner scanner) {
        System.out.print("请输入账单ID: ");
        int id = scanner.nextInt();
        Bill bill = billManager.searchBillById(id);
        if (bill != null) {
            System.out.println("找到账单: " + bill);
        } else {
            System.out.println("未找到账单ID: " + id);
        }
    }
    static class BudgetManager {
        private double monthlyBudget;
        private boolean isBudgetSet;

        public BudgetManager() {
            this.monthlyBudget = 0.0;
            this.isBudgetSet = false;
        }

        public void setMonthlyBudget(double budget) {
            this.monthlyBudget = budget;
            this.isBudgetSet = true;
        }

        public double getMonthlyBudget() {
            return monthlyBudget;
        }

        public boolean isBudgetSet() {
            return isBudgetSet;
        }
    }
    static class BillManager {
        private List<Income> incomes = new ArrayList<>();
        private List<Expense> expenses = new ArrayList<>();
        private List<Bill> bills = new ArrayList<>();
        private BudgetManager budgetManager = new BudgetManager();

        public BillManager() {}

        public void addIncome(Income income) {
            incomes.add(income);
            bills.add(new Bill(income.getId(), "收入", income.getAmount(), income.getDate().toString()));
        }

        public void addExpense(Expense expense) {
            expenses.add(expense);
            bills.add(new Bill(expense.getId(), "支出", expense.getAmount(), expense.getDate().toString()));
        }

        public List<Income> getIncomes() {
            return incomes;
        }

        public List<Expense> getExpenses() {
            return expenses;
        }

        public List<Bill> getAllBills() {
            return bills;
        }

        public Bill searchBillById(int id) {
            for (Bill bill : bills) {
                if (bill.getId() == id) {
                    return bill;
                }
            }
            return null;
        }

        public void setMonthlyBudget(double budget) {
            budgetManager.setMonthlyBudget(budget);
        }

        public double getMonthlyBudget() {
            return budgetManager.getMonthlyBudget();
        }

        public boolean isBudgetSet() {
            return budgetManager.isBudgetSet();
        }

        public void monthlyStatistics() {
            double totalIncome = 0.0;
            double totalExpense = 0.0;
            double remainingBudget = 0.0;

            for (Income income : incomes) {
                totalIncome += income.getAmount();
            }

            for (Expense expense : expenses) {
                totalExpense += expense.getAmount();
            }

            if (isBudgetSet()) {
                remainingBudget = getMonthlyBudget() - totalExpense;
            }

            System.out.println("月度统计:");
            System.out.println("总收入: " + totalIncome);
            System.out.println("总支出: " + totalExpense);
            if (isBudgetSet()) {
                System.out.println("月度预算: " + getMonthlyBudget());
                System.out.println("剩余预算: " + remainingBudget);
            } else {
                System.out.println("未设置月度预算。");
            }
        }
    }
    static class Bill {
        private int id;
        private String type;
        private double amount;
        private String date;

        public Bill(int id, String type, double amount, String date) {
            this.id = id;
            this.type = type;
            this.amount = amount;
            this.date = date;
        }

        public int getId() {
            return id;
        }

        public String getType() {
            return type;
        }

        public double getAmount() {
            return amount;
        }

        public String getDate() {
            return date;
        }

        @Override
        public String toString() {
            return "Bill{" +
                    "id=" + id +
                    ", type='" + type + '\'' +
                    ", amount=" + amount +
                    ", date='" + date + '\'' +
                    '}';
        }
    }

    static class Income {
        private int id;
        private LocalDate date;
        private double amount;
        private String category;
        private String note;

        public Income(LocalDate date, double amount, String category, String note) {
            this.id = generateId();
            this.date = date;
            this.amount = amount;
            this.category = category;
            this.note = note;
        }

        private int generateId() {
            return (int) (Math.random() * 10000); // 生成随机ID
        }

        public LocalDate getDate() {
            return date;
        }

        public void setDate(LocalDate date) {
            this.date = date;
        }

        public double getAmount() {
            return amount;
        }

        public void setAmount(double amount) {
            this.amount = amount;
        }

        public String getCategory() {
            return category;
        }

        public void setCategory(String category) {
            this.category = category;
        }

        public String getNote() {
            return note;
        }

        public void setNote(String note) {
            this.note = note;
        }

        public int getId() {
            return id;
        }
    }

    static class Expense {
        private int id;
        private LocalDate date;
        private double amount;
        private String category;
        private String note;

        public Expense(LocalDate date, double amount, String category, String note) {
            this.id = generateId();
            this.date = date;
            this.amount = amount;
            this.category = category;
            this.note = note;
        }

        private int generateId() {
            return (int) (Math.random() * 10000); // 生成随机ID
        }

        public LocalDate getDate() {
            return date;
        }

        public void setDate(LocalDate date) {
            this.date = date;
        }

        public double getAmount() {
            return amount;
        }

        public void setAmount(double amount) {
            this.amount = amount;
        }

        public String getCategory() {
            return category;
        }

        public void setCategory(String category) {
            this.category = category;
        }

        public String getNote() {
            return note;
        }

        public void setNote(String note) {
            this.note = note;
        }

        public int getId() {
            return id;
        }
    }
}